/**
 * Filename：Response.java Created by: qgl Created on: 2008-12-29 下午03:11:23 Last
 * modified by：$Author$ Last modified on: $Date$ Revision: $Revision$
 */
package com.ibiaoke.common.response;


import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.ToString;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 返回信息类
 * <p>
 * Created by huhaichao on 2017/5/11.
 */
@Getter
@ToString
public class Res {

    private int code = Code.Ok;
    private String message;
    private long timestamp=System.currentTimeMillis();
    private Map<String, Object> response = new HashMap<>();

    /**
     * 返回json视图
     *
     * @return
     */
    public ModelAndView toJsonView() {
        ModelAndView mav = new ModelAndView();

        MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
        //调用两次转json去除空属性
        JSONObject jsonObject = (JSONObject) JSONObject.parse(toJson());
        jsonView.setAttributesMap(jsonObject);

        mav.setView(jsonView);
        return mav;

    }


    public String toJson(){
        return JSONObject.toJSON(getAttributeMap()).toString();
    }

    private Map<String, Object> getAttributeMap() {
        Map<String, Object> attributes = new HashMap<String, Object>();
        if (code == Code.Ok) {
            attributes.put("code", code);
            attributes.put("response", response);
        } else {
            attributes.put("code", code);
            attributes.put("message", message);
        }
        return attributes;
    }


    /**
     * 设置错误信息
     *
     * @param errorMessage
     * @return
     */
    public Res addError(String errorMessage) {

        message = errorMessage;
        this.code = Code.Error; // 设置为错误
        response.clear();

        return this;
    }

    /**
     * 增加返回信息
     *
     * @param key
     * @param value
     * @return
     */
    public Res addResponse(String key, Object value) {
        response.put(key, value);
        return this;
    }

    public void write(HttpServletResponse response) {
        try {
            PrintWriter out = response.getWriter();
            out.print(toJson());
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 响应状态码
     */
    public interface Code {
        int Ok=200;//正常
        int Redirect=302;
        int Verify=403;//权限禁止
        int Error=500;//服务器错误
    }

    public static Res createErrorRes(String errorMessage) {
        return new Res().addError(errorMessage);
    }
    public static Res createRes(String errorMessage,int code) {
        return new Res().addError(errorMessage);
    }

    public static Res create() {
        return new Res();
    }




}